package cn.jd.system.service.impl;

import cn.jd.basic.query.BaseQuery;
import cn.jd.basic.util.PageList;
import cn.jd.system.domain.Menu;
import cn.jd.system.domain.Role;
import cn.jd.system.domain.RoleMenu;
import cn.jd.system.domain.RolePermission;
import cn.jd.system.mapper.MenuMapper;
import cn.jd.system.mapper.PermissionMapper;
import cn.jd.system.mapper.RoleMenuMapper;
import cn.jd.system.mapper.RolePermissionMapper;
import cn.jd.system.service.IRoleService;
import cn.jd.basic.service.impl.BasicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jd
 * @since 2022-10-24
 */
@Service
public class RoleServiceImpl extends BasicServiceImpl<Role> implements IRoleService {
      @Autowired
      private MenuMapper menuMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public void remove(Long id) {
        roleMenuMapper.removeByRoleId(id);
        rolePermissionMapper.removeByRoleId(id);
        super.remove(id);
    }

    @Override
    public void update(Role role) {
        System.out.println(role);
        roleMenuMapper.removeByRoleId( role.getId());
        rolePermissionMapper.removeByRoleId( role.getId());
        roleMenuMapper.addRoleMenu(role.getId(),role.getMenus());
        rolePermissionMapper.addRolePermissions(role.getId(),role.getPermissions());

        super.update(role);
    }

    @Override
    public void save(Role role) {
        super.save(role);
        roleMenuMapper.addRoleMenu(role.getId(),role.getMenus());
        rolePermissionMapper.addRolePermissions(role.getId(),role.getPermissions());
    }

    @Override
    public PageList<Role> queryByPage(BaseQuery query) {
        // 分页查询所有数据
        PageList<Role> rolePageList = super.queryByPage(query);
        List<Role> roleList = rolePageList.getRows();
        /*List<Long> ids = roleList.stream().map(Role::getId).collect(Collectors.toList());*/
       /* System.out.println(ids);*/


        // 按钮权限id集合返回
        List<RolePermission> rolePermissionMapperAll = rolePermissionMapper.findAll();
        /*Map<Long, Department> collect = allDepts.stream().collect(Collectors.toMap(Department::getId, dept -> dept));*/
        Map<Long, List<Long>> collect = rolePermissionMapperAll.stream().collect(Collectors.groupingBy(RolePermission::getRoleId, Collectors.mapping(RolePermission::getPermissionId, Collectors.toList())));
      /*  System.out.println(collect+"collect");*/

        // 菜单权限id集合返回
        List<RoleMenu> roleMenuMapperAll = roleMenuMapper.findAll();
        Map<Long, List<Long>> roleMenuMap = roleMenuMapperAll.stream().collect(Collectors.groupingBy(RoleMenu::getRoleId,
                Collectors.mapping(RoleMenu::getMenuId, Collectors.toList())));

       /* System.out.println(roleMenuMap);*/

        // 查询所有菜单
       /* List<Menu> menuMapperAll = menuMapper.findAll();
        Map<Long, Menu> collect1 = menuMapperAll.stream().collect(Collectors.toMap(Menu::getId, me -> me));

        System.out.println(collect1);
        for (Role role : roleList) {
            Menu menu = collect1.get(role.getMenuId());
            System.out.println(menu);
            // role.setOwnMenus(menuMapperAll);
          *//* role.setOwnMenus(menu);*//*
        }*/


       /* HashMap<Long, List<Long>> menuMap = new HashMap<>();*/
        List<Menu> menuList = menuMapper.findAll();
        Map<Long, List<Menu>> menuMap2 =new HashMap<>();

        for (Map.Entry<Long, List<Long>> listEntry : roleMenuMap .entrySet()) {

            List<Menu> collect2 = menuList.stream().filter(menu -> listEntry.getValue().contains(menu.getId())).collect(Collectors.toList());
            menuMap2.put(listEntry.getKey(),collect2);
        }



        for (Role role : roleList) {
            role.setPermissions(collect.get(role.getId()));
            role.setMenus(roleMenuMap.get(role.getId()));
           /* role.setOwnMenus(menuMapper.findbyRoleId(role.getId()));*/
           role.setOwnMenus(menuMap2.get(role.getId()));
        }
        return rolePageList;
    }
}
